package com.customer.controller;

import com.customer.service.CustomerLogin;
import com.customer.service.RedisService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**
 * 2 * @Author: xy
 * 3 * @Date: 2021/10/11 下午 8:42:48
 */
@RestController
@RequestMapping("/customer")
public class CustomerController extends Thread {
    @Autowired
    private CustomerLogin customerLogin;
    @Autowired
    private HttpServletRequest request;

    @RequestMapping("/login")
    public ModelAndView loginView(String phone, String code) {
        ModelAndView mv = new ModelAndView();

        int flag = customerLogin.login(phone, code);
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("code", code);
        if (flag == 0) {
            map.put("status", "登陆成功");
            mv.addObject("msg", "登录成功");
            redisService.hmset(phone, map);
        } else if (flag == -1) {
            mv.addObject("error", "检查验证码");
            mv.setViewName("/customer/index");
        } else {
            /**
             * 登陆请求的参数中, 有一个参数唯一标识一个 手机号,
             * 用这个参数作为key存入redis, 对应的value为登陆错误的次数, string 类型,
             * 并设置过期时间为 1min. 当获取到的 value == "2" , 说明当前请求为第 3 次登陆异常, 锁定.
             * 所谓的锁定, 就是将对应的value设置为某个标识符, 比如"lock", 并设置过期时间为 1h
             */

            if (redisService.hget(("frequency"), "lock") != null & redisService.hget(("frequency2"), "lock") != null) {
                redisService.hset("frequency3", "lock", 3, 86400);
                mv.addObject("three", 3);
                try {
                    //      代码优化
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    redisService.del(phone);
                }
            }
            if (redisService.hget(("frequency"), "lock") == null) {
                redisService.hset("frequency", "lock", 1, 60);
                mv.addObject("index", 1);
            } else if (redisService.hget(("frequency"), "lock") != null) {
                redisService.hset("frequency2", "lock", 2, 180);
                mv.addObject("second", 2);
            }

        }
        mv.setViewName("forward:/customer/index.jsp");
        return mv;
    }

    @ResponseBody
    @RequestMapping("/sendSms")
    public String sendSms(String phone) {
        String code = RandomStringUtils.randomNumeric(6);
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("code", code);
        map.put("status", "未登陆");
        redisService.hmset(phone, map);
        System.out.println("验证码是" + "" + code);
        return code;
    }

    @Autowired
    private RedisService redisService;

    @ResponseBody
    @RequestMapping(value = "/sendSms2", produces = "application/json; charset=utf-8")
    public String sendSms2(String phoneNumber) {
        String code = RandomStringUtils.randomNumeric(6);
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phoneNumber);
        map.put("code", code);
        redisService.hmset("verification", map, 60);
        System.out.println("验证码是" + "" + code);
        String codetime = "验证码是" + code + "有效时长60S";
        return codetime;
    }
}
